package com.haofengsoft.scce.ui.fragment;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.haofengsoft.platform.companent.scrollview.PagerSlidingLumpTabStrip;
import com.haofengsoft.scce.R;
import com.haofengsoft.scce.adapter.information.InformationAttentionAdapter;
import com.haofengsoft.scce.adapter.information.InformationAttentionModel;
import com.haofengsoft.scce.controller.AttentionController;
import com.haofengsoft.scce.ui.consultation.ManageAttentionActivity;

/**
 * 资讯
 *
 * @author zhj
 */
@SuppressLint("NewApi")
public class InformationFragment extends BaseFragment {

    private PagerSlidingLumpTabStrip informationTabPsts;
    private ViewPager informationVp;

    private InformationAttentionAdapter pagerAdapter = null;
    private List<InformationAttentionModel> informationAttentionModelList;
    private Context context;
    private int currItemId = 0;
    private int currIndex = 0;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.main_information, container, false);
        view.setOnTouchListener(new DontSpillOnTouchListener());
        context = getActivity();
        informationTabPsts = (PagerSlidingLumpTabStrip) view.findViewById(R.id.information_attention_tab_psts);
        informationVp = (ViewPager) view.findViewById(R.id.information_body_vp);
        informationTabPsts.setIndicatorColorResource(R.color.common_btn_nor);
        informationTabPsts.setIndicatorRadiusDp(getActivity().getApplicationContext(), 10);
        ImageView addAttentionIv = (ImageView) view.findViewById(R.id.information_add_attention_iv);
        addAttentionIv.setOnClickListener(new AddAttentionClickListener());
        return view;
    }

    // 请求网络的 获取选中

    @Override
    public void onResume() {
        super.onResume();
        if (!isHidden()) {
            //判读之前选中项目 是否存在
            boolean beforeCurrItem = false;
            informationAttentionModelList = AttentionController.getInstance(context).convertAttentionList();
            for (int k = 0; k < informationAttentionModelList.size(); k++) {
                if (currItemId == informationAttentionModelList.get(k).getId()) {
                    currIndex = k;
                    beforeCurrItem = true;
                }
                //上次选中的，这次删除了
            }
            if (!beforeCurrItem) {
                currIndex = 0;
            }
//            informationVp.removeAllViews();//涉及到实时刷新,所以要将之前的布局清空掉。
//            informationVp.removeAllViewsInLayout();//removeAllViews();//赋值之前先将Adapter中的
            pagerAdapter = new InformationAttentionAdapter(getChildFragmentManager(), informationAttentionModelList);
            informationVp.setAdapter(pagerAdapter);
            informationVp.setCurrentItem(currIndex);
            informationTabPsts.setViewPager(informationVp);
            informationTabPsts.notifyDataSetChanged();
        }
    }


    @Override
    protected void lazyLoad() {

    }

    class AddAttentionClickListener implements OnClickListener {
        @Override
        public void onClick(View view) {
            if (pagerAdapter != null) {
                currIndex = informationVp.getCurrentItem();
                currItemId = informationAttentionModelList.get(currIndex).getId();
            }
            Intent addIntent = new Intent(getActivity(), ManageAttentionActivity.class);
            startActivity(addIntent);
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (pagerAdapter != null) {
            currIndex = informationVp.getCurrentItem();
            currItemId = informationAttentionModelList.get(currIndex).getId();
        }
    }
}
